#!/usr/local/bin/python

# __    _____ _____ _____    ___     ___ 
#|  |  |  _  |     |   __|  |_  |   |   |
#|  |__|     | | | |__   |   _| |_ _| | |
#|_____|__|__|_|_|_|_____|  |_____|_|___|

# Script to return average call duration
# Author: Carl Javier 4088697
# Date: 21 September 2008

#Uses Call record from Asterisk to calculate
#the average call duration since the beginning 
#of the file.
#Will return a value to stdout

import sys, os


jailpwd=""
callrecord="/var/log/asterisk/cdr-csv/Master.csv"

#Show only the last 100 calls
command = "cut -d \",\" -f 13 " + jailpwd+callrecord + " |tail -100"
#print command
fileOpen = os.popen(command,'r')

total = 0
counter = 0
for x in fileOpen:
	#print x[:len(x)-1] #-1 refers to removing the \n character
	total += int(x)
	counter+=1


#print "Total is " + str(total) + " minutes \n" 
#print "Average Call duration is: " + str((total/counter))

#Print out Call Average in seconds
print str((total/counter))

fileOpen.close()

